package com.lovo.homework0201.interceptor;



import com.lovo.homework0201.entity.UserPowerEntity;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 登录拦截
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /*//前端把路径最后的权限字符串放在header里，后端可以直接通过header获取请求权限
        String power = request.getHeader("power");*/
        //获取请求地址
        String requestURI = request.getRequestURI();
        String substring = requestURI.substring(requestURI.lastIndexOf("/"));
        boolean permitted = SecurityUtils.getSubject().isPermitted(substring);
        //response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        if (!permitted){
            response.getWriter().print("没有权限");
        }
        return permitted;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        //应用控制器处理完后，前端控制器渲染视图前调用
        //System.out.println("postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //前端控制器渲染完视图后进行调用
        //System.out.println("afterCompletion");
    }
}
